package com.apexedu.sys.service.impl;

import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.sys.action.SysBizLogAction;
import com.apexedu.sys.entity.TSysBizLog;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.sys.util.SQLConfigUtil;
import com.apexedu.util.DateUtil;
import com.apexedu.util.StringUtil;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class SysBizLogServiceImpl extends AbstractBaseServiceSupport {

	/**
	 * 操作日志显示
	 */
	public List<?> list(Object myaction, Pager pager) throws Exception {
		SysBizLogAction action = (SysBizLogAction) myaction;
		TSysBizLog tsysBizLog = action.getTsysBizLog();

        String hql;
        if( "sysadmin".equals(action.getLoginuser().getUserloginid()) ){
            hql = " from TSysBizLog a where 1=1 ";
        }else{
            hql = " from TSysBizLog a where a.czr!='sysadmin' ";
        }
		List<Object> param = new LinkedList<Object>();
		if (tsysBizLog != null) {
			if (tsysBizLog.getCzr() != null && !"".equals(tsysBizLog.getCzr())) {
				hql += " and a.czr like ?";
				param.add("%" + tsysBizLog.getCzr() + "%");
			}
			if (tsysBizLog.getCzdx() != null && !"".equals(tsysBizLog.getCzdx())) {
				hql += " and a.czdx = ?";
				param.add( tsysBizLog.getCzdx() );
			}
			if (tsysBizLog.getCzlx() != null && !"".equals(tsysBizLog.getCzlx())) {
				hql += " and a.czlx = ?";
				param.add( tsysBizLog.getCzlx() );
			}
		}
		/*
		 * 操作时间查询过滤(时间段),qssj为起始时间,zzsj为终止时间
		 */
		if (StringUtil.isNotBlank(action.getParameter("qssj")) && StringUtil.isNotBlank(action.getParameter("zzsj"))) {
			action.setParameter("qssj", action.getParameter("qssj"));
			action.setParameter("zzsj", action.getParameter("zzsj"));
			hql += " and a.czsj between ? and ?";
			Date qssj = DateUtil.parseDate(action.getParameter("qssj") + ":00");
			Date zzsj = DateUtil.parseDate(action.getParameter("zzsj") + ":59");
			param.add( qssj );
			param.add( zzsj );
		}
		long c = getBaseDao().count(" select count(*) as c " + hql, param.toArray());
		pager.setTotalRows(c);
		String hqlcolumn = "select new TSysBizLog(a.wid, a.czsj, (select zdmc from TSysCode where zdlb='SYSLOG_CZDX' and zdbm=a.czdx) as czdx," +
		"a.czr, (select zdmc from TSysCode where zdlb='SYSLOG_CZLX' and zdbm=a.czlx) as czlx, a.czdx, a.czlx, a.clientIp, a.czms)";
		hql = hqlcolumn + hql + " order by czsj desc";
		List<?> list = getBaseDao().findPageByHql(hql, pager, param.toArray());
		return list;
	}

	public boolean remove(Object myaction) throws Exception {
		SysBizLogAction action = (SysBizLogAction) myaction;
		boolean deleteSuccess = false;
		String ids = action.getWid();
		if (ids != null) {
			deleteSuccess = getBaseDao().deleteAll("TSysBizLog", "wid", "=", ids);
		} else {
			String czsj = action.getParameter("czsj");
			String sql = SQLConfigUtil.getSql("sql.bizlog.deleteAll");
			deleteSuccess = DBUtil.executeSQL(sql, czsj) > 0;
		}
		return deleteSuccess;
	}
	public void load(Object arg0) throws Exception {
	}
    public void saveOrUpdate(Object arg0) throws Exception {
    }

    /**
     * 这个是提供给定时任务，每5分钟批量保存操作日志的。
     * fixedRate 从上一个任务开始到下一个任务开始的间隔，单位是毫秒；
     * fixedDelay 表示从上一个任务完成开始到下一个任务开始的间隔，单位是毫秒；
     * cron 一个周期表达式。如指定每天17:03:00执行：cron="0 03 17 * * ?"
     * @throws Exception
     */
    public void saveBatch(){
        BusinessLogUtil.saveBatch();
    }
}
